
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/head.ftl">
    <style>
        #ContentModal img{
            width: 100%;
        }
        #ContentModal .modal-body{
            overflow-x: auto;
            max-height: 500px;
        }
    </style>


</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <#assign menu = "strategyDetail"/>
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略管理</h1>
                </div>
            </div>
            <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" id="currentPage" name="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>
                <button id="btn_query" class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>

                <a role="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加攻略文章
                </a>
            </form>

            <table class="table table-striped table-hover" >
                <tr>
                <tr>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>

                </tr>

                </tr>

                <#list pageInfo.list as s>
                <#--遍历查询得到的每一个domain对象-->
                    <tr>
                        <td>${s.title!}</td>
                        <td><img src="${s.coverUrl!}" width="40px"></td>
                        <td>${(s.releaseTime?string("yyyy-MM-dd"))!}</td>
                        <td>${(s.sequence)!}</td>
                        <td>${(s.catalog.name)!}</td>
                        <td>${s.stateDisplay!}</td>
                        <td>
                            <a role="button" class="inputBtn" data-json='${s.json}'>修改</a>
                        </td>
                    </tr>
                </#list>

            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>


<div id="strategyDetailModal" class="modal fade" tabindex="-1" role="dialog" >
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal " method="post" enctype="multipart/form-data" action="/strategyDetail/saveOrUpdate.do" style="margin: -3px 20px">
                    <input id="id" type="hidden" name="id"/>
                    <div class="form-group inline">
                        <label class="col-sm-4 control-label">标题</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="title" name="title" placeholder="攻略标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="200px" src=""/>
                            <input id="strategyCoverUrl" type="hidden" name="coverUrl"/>
                            <input type="file" class="form-control" id="multipartFile" name="multipartFile" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略</label>
                        <div class="col-sm-6">
                            <select id="strategySelect" name="strategy.id" class="form-control form-control-chosen" data-placeholder="请选择所属攻略">
                                <option value="-1">----请选择----</option>
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别</label>
                        <div class="col-sm-6">
                            <select id="catalogSelect" name="catalog.id" class="form-control form-control-chosen" data-placeholder="请选择攻略类别">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否发布</label>
                        <div class="col-sm-6">
                            <select id="state" class="form-control" autocomplete="off" name="state">
                                <option value="1">发布</option>
                                <option value="2">待审核</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">排序</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sequence" name="sequence" placeholder="排序">
                        </div>
                    </div>
                    <textarea id="content" name="content.content" class="ckeditor"  cols="10"  rows="10">

                    </textarea>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn btn-success" id="saveBtn" aria-hidden="true">保存</a>
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>


<script>
    $(function () {
        //富文本编辑框
        var ck = CKEDITOR.replace( 'content.content',{
            //图片预览服务器:存放之前传过图片
            filebrowserBrowseUrl: 'http://image.so.com',
            //图片上传请求路径
            filebrowserUploadUrl: '/uploadImage.do'
        });


        $(".inputBtn").click(function () {

            //ck.setData("");

            $("#coverUrl").attr("src","");
            $("#strategyDetailModal").clearForm(true);
            var json = $(this).data("json");
            //console.log(json);
            if(json){
                $("#id").val(json.id);
                $("#title").val(json.title);
                $("#subTitle").val(json.subTitle);
                $("#placeId").val(json.placeId);
                $("#coverUrl").attr("src",json.coverUrl);
                $("#strategyCoverUrl").val(json.coverUrl);
                $("#state").val(json.state);
                $("#sequence").val(json.sequence);
                //回显当前的所属攻略,在detail类中添加strategyId的属性
                $("#strategySelect").val(json.strategyId);




                $.get("/strategyDetail/getStrategyCatalogByStrategyId.do",{strategyId:json.strategyId},function (data) {
                    var html="<option value='-1'>----请选择----</option>"
                    $.each(data,function (index, ele) {
                        html+="<option value='"+ele.id+"'>"+ele.name+"</option>"
                    });
                    $("#catalogSelect").html(html);
                    $("#catalogSelect").val(json.catalogId);
                })

                //发送异步请求去获取文章的内容
                $.get("/strategyDetail/getConteneByDetailId.do",{detailId:json.id},function (data) {
                    console.log(data);
                    ck.setData(data.content);

                })

            }



            //值改变事件
            $("#strategySelect").change(function () {
                var value = $(this).val();
                if(value==-1){
                    var html = "<option value='-1'>----请选择----</option>"
                    $("#catalogSelect").html(html);
                    return ;
                }

                $.get("/strategyDetail/getStrategyCatalogByStrategyId.do",{strategyId:value},function (data) {
                    var html="<option value='-1'>----请选择----</option>"
                    $.each(data,function (index, ele) {
                        html+="<option value='"+ele.id+"'>"+ele.name+"</option>"
                    })
                    $("#catalogSelect").html(html);
                })
            })

            //js选择图片后的回显
            $("#multipartFile").change(function (e) {
                for (var i = 0; i < e.target.files.length; i++) {
                    var file = e.target.files.item(i);
                    console.log(file);
                    if (!(/^image\/.*$/i.test(file.type))) {
                        continue; //不是图片 就跳出这一次循环
                    }
                    //实例化FileReader API
                    var freader = new FileReader();
                    freader.readAsDataURL(file);
                    freader.onload = function(e) {
                        $("#coverUrl").attr("src",e.target.result);
                    }
                }
            });
            $("#strategyDetailModal").modal("show");

        })


        //点击保存
        $("#saveBtn").click(function () {

            //设置富文本框中的值
            var value = ck.getData();
            console.log(value);
            $("#content").val(value);


            $("#editForm").ajaxSubmit(function (data) {
                if(data.success){
                    location.reload();
                }else{
                    $.messager.alert("温馨提示",data.msg);
                }
            })
        })


    })
</script>
</body>
</html>